package com.skz.job.spider;

import com.skz.job.domain.Job;
import com.skz.job.util.LocationUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;
import java.util.Map;

/**
 * @author: 宋开宗
 * @create: 2019-02-18 22:55
 **/
public class BossZhiPinSpider implements PageProcessor {
    private static final String JOB_DETAIL_MATCH = "job_detail";
    private static final String CAPTCHA_TEXT = "为了您的账号安全，我们需要在执行操作之前验证您的身份，请输入验证码。";
    private String listUrl;
    private int listStartPage;
    private static int totalDetailPages = 0;
    private String lastValidUrl;

    public BossZhiPinSpider(String listUrl, int listStartPage) {
        this.listUrl = listUrl;
        this.listStartPage = listStartPage;
    }

    @Override
    public void process(Page page) {
        if (page.getHtml().get().contains(CAPTCHA_TEXT)) {
            System.out.println("出现验证码,停止爬虫。");
            page.addTargetRequest(lastValidUrl);
            return;
        }
        if (page.getUrl().get().contains(JOB_DETAIL_MATCH)) {
            totalDetailPages++;
            String companyName = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[1]/div[2]/div/a[2]/text()").get();
            String trade = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[1]/div[2]/p[4]/a/text()").get();
            String description = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[1]/div/text()").get();
            String location = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[6]/div/div[2]/img/@src").get();
            if (location == null) {
                location = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[5]/div/div[2]/img/@src").get();
            }
            if (location == null) {
                location = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[4]/div/div[2]/img/@src").get();
            }
            String address = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[6]/div/div[1]/text()").get();
            if (address == null) {
                address = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[5]/div/div[1]/text()").get();
            }
            if (address == null) {
                address = page.getHtml().xpath("//*[@id=\"main\"]/div[3]/div/div[2]/div[2]/div[4]/div/div[1]/text()").get();
            }
            //经纬度
            String[] yx = location.substring(location.indexOf("A:") + 2, location.lastIndexOf("&key")).split(",");
            Map<String, String> locationMap = LocationUtils.parse(yx[1], yx[0]);
            Job job = new Job(companyName, trade, description, yx[1] + "," + yx[0], locationMap.get("district"), locationMap.get("address"), address, page.getUrl().get(), "boss");
            page.putField("job", job);
            try {
                Thread.sleep((long) (2000 + Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lastValidUrl = page.getUrl().get();
        } else {
            List<String> links = page.getHtml().css("#main > div > div.job-list > ul").links().all();
            if (links.size() != 0) {
                links.forEach(s -> {
                    if (s.contains(JOB_DETAIL_MATCH)) {
                        page.addTargetRequest(s);
                    }
                });
                listStartPage++;
                page.addTargetRequest(listUrl + listStartPage);
            }
        }
        if (totalDetailPages % 10 == 0) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3);
    }


}